<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>客户管理</title>
<link href="../../../static/css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../../static/js/jquery.js"></script>
<script type="text/javascript">
function deltipOpen(purchaseId) {
	$("#deltip").fadeIn(200);
	let str = '      <input name="" type="button"  class="sure" value="确定" onclick="deltipClose(\''+purchaseId+'\')" />\n' +
            '      &nbsp;\n' +
            '      <input name="" type="button"  class="cancel" value="取消" onclick="deltipClose('+null+')" />';
	$("#pdl_Dtip").html(str)
}
function deltipClose(purchaseId) {
    if (purchaseId != null && purchaseId !==''){
      $.ajax({
        url:"/purchase/deleteOnePurchase",
        method: "post",
        data:{purchaseId:purchaseId},
        dataType: "JSON",
        success:function (rel) {
          if (rel.data){
            window.location.reload();
          }
        }
      })
    }
	$("#deltip").fadeOut(200);
}
function examinetipOpen(purchaseId,purchaseStatus) {
    getDeptCP();
	$("#examinetip").fadeIn(200);
    let str = '      <input name="" type="button"  class="sure" value="确定" onclick="examinetipclose(\''+purchaseId+'\','+purchaseStatus+')" />\n' +
          '      &nbsp;\n' +
          '      <input name="" type="button"  class="cancel" value="取消" onclick="examinetipclose('+null+','+null+')" />';
    $("#pdl_Utip").html(str);

}
function examinetipclose(purchaseId,purchaseStatus) {
    var checkId = $(".userId").val();
    if (purchaseId != null && purchaseStatus != null) {
      var status = purchaseStatus;
      if (status == 1){
        purchaseStatus = 2;
      }
      $.ajax({
        url: '/purchase/updatePurchaseStatus',
        method: 'post',
        data: {purchaseId: purchaseId, checkStatus: purchaseStatus, checkId: checkId},
        dataType: "JSON",
        success: function (rel) {
          if (rel.data) {
            window.location.reload();
          }
        }
      })
    }

	$("#examinetip").fadeOut(200);
}
window.deptInfo = {
  deptJobInfo:[]
}
//获取部门职位人员
function getDeptCP(){
  $.ajax({
    url:"/commonDeptJobUser",
    method:"get",
    success:function (rel) {
      window.deptInfo.deptJobInfo = rel;
      let str = '<option value="">请选择</option>\n';
      for (let i = 0; i < rel.data.length; i++) {
        str += '<option value="'+rel.data[i].deptId+'">'+rel.data[i].deptName+'</option>\n';
      }
      $(".deptId").html(str);
    }
  })
}
//当部门发生改变
function deptChangeP(){
  var depts = window.deptInfo.deptJobInfo.data;
  var deptId = $(".deptId").val();
  let str = '<option value="">请选择</option>\n';
  var jobs=[];
  for (let j=0;j<depts.length;j++) {
    if (deptId == depts[j].deptId) {
      jobs = depts[j].jobs;
      break;
    }
  }
  if (deptId !== "") {
    for (let i = 0; i < jobs.length; i++) {
      str += '<option value="' + jobs[i].jobId + '">' + jobs[i].jobName + '</option>'
    }
  }
  $(".jobId").html(str);
}
//当部门发生改变
function jobChangeP(){
  var depts = window.deptInfo.deptJobInfo.data;
  var deptId = $(".deptId").val();
  var users;
  let str = '<option value="">请选择</option>\n';
  var jobs = [];
  for (let j=0;j<depts.length;j++) {
    if (deptId == depts[j].deptId) {
      jobs = depts[j].jobs;
      break;
    }
  }
  if (jobs.length>0){
    var jobId = $(".jobId").val();
    for (let i = 0; i < jobs.length; i++) {
      if (jobId == jobs[i].jobId) {
        users = jobs[i].users;
      }
    }
  }
  for (let i = 0; i < users.length; i++) {
    if (deptId == users[i].deptId){
      str+='<option value="'+users[i].uid+'">'+users[i].uname+'</option>\n';
    }
  }
  $(".userId").html(str);
}
//全局信息
let pageSize = 5;
var conn = {
  purchaseId:null,
  prePurchaseTime:null,
  afterPurchaseTime:null,
  preTotalMoney:null,
  afterTotalMoney:null,
  checkStatus:null
};

function getParameter() {
  var cs = $("#check_Status").val();
  if ("请选择"===cs){
    cs = null;
  }

  conn = {
    purchaseId:$("#purchase_ID").val(),
    prePurchaseTime:$("#preTime").val(),
    afterPurchaseTime:$("#afterTime").val(),
    preTotalMoney:$("#preMoney").val(),
    afterTotalMoney:$("#afterMoney").val(),
    checkStatus:cs
  };
}


function search(){
  getParameter();
  queryPurchase(1);
}

$(function () {
  getParameter();
  queryPurchase(1);
})

function queryPurchase(currentPage) {
  conn.pageNum = currentPage;
  conn.pageSize = pageSize;
  $.ajax({
    url:"/purchase/queryPurchasePage",
    method:"post",
    data:conn,
    dataType:"JSON",
    success:function (rel) {
      console.log(conn);
      let str = '';
      rel.data.data.forEach(function (purchase,i) {
        //对审核状态做处理
        let checkStatusName;
        if (purchase.checkStatus === "1"){
          checkStatusName = "未审核";
        }else if (purchase.checkStatus === "2"){
          checkStatusName = "审核中";
        }else if (purchase.checkStatus === "3"){
          checkStatusName = "审核通过";
        }else {
          checkStatusName = "审核未通过";
        }
        //数据显示
        str +=
                '<tr>\n' +
                '          <td>'+((++i)+(currentPage-1)*pageSize)+'</td>\n' +
                '          <td>'+purchase.purchaseId+'</td>\n' +
                '          <td>'+purchase.purchaseTime+'</td>\n' +
                '          <td>'+purchase.totalMoney+'</td>\n' +
                '          <td>'+purchase.creatName+'</td>\n' +
                '          <td>'+checkStatusName+'</td>\n' +
                '          <td>'+purchase.checkName+'</td>\n' +
                '          <td>'+purchase.checkTime+'</td>\n';
        if (purchase.checkStatus === "1"){
          str +=  '          <td>\n' +
                  '            <a href="purchase/purchaseUpdate/'+purchase.purchaseId+'" class="tablelink">修改</a>\n' +
                  '            <a href="javascript:void(0);" class="tablelink" onclick="deltipOpen(\''+purchase.purchaseId+'\')">删除</a>\n' +
                  '            <a href="javascript:void(0);" class="tablelink" onclick="examinetipOpen(\''+purchase.purchaseId+'\','+purchase.checkStatus+')">提交审核</a>\n' +
                  '            </td>\n' +
                  '        </tr>';
        }else if (purchase.checkStatus === "2"){
          str += '<td>\n' +
                  '          \t<a href="purchase/purchaseView?purchaseid='+purchase.purchaseId+'" class="tablelink">查看详情</a>\n' +
                  '          </td>\n' +
                  '        </tr>'
        }else if (purchase.checkStatus === "3"){
          str += '<td>\n' +
                  '          \t<a href="purchase/purchaseView?purchaseid='+purchase.purchaseId+'" class="tablelink">查看详情</a>\n' +
                  '            <a href="purchase/purchaseView?purchaseid='+purchase.purchaseId+'" class="tablelink">入库详情</a>\n' +
                  '          </td>\n' +
                  '        </tr>'
        }else {
          str += '<td>\n' +
                  '            <a href="purchase/purchaseUpdate/'+purchase.purchaseId+'" class="tablelink">修改</a>\n' +
                  '            <a href="javascript:void(0);" class="tablelink" onclick="examinetipOpen(\''+purchase.purchaseId+'\','+purchase.checkStatus+')">提交审核</a>\n' +
                  '          </td>\n' +
                  '        </tr>'
        }
      })
      $("#purchaseBody").html(str);
      //分页显示
      let strbu = '<div class="message">共<i class="blue">'+rel.data.totalCount+'</i>条记录，当前显示第<i class="blue">'+currentPage+'</i>页</div>';
      let beforeNum;
      if (currentPage>1){
        beforeNum = currentPage -1;
      }else {
        beforeNum = 1;
      }
      strbu +='<ul class="paginList">\n' +
              '   <li class="paginItem"><a href="javascript:;" onclick="queryPurchase('+beforeNum+')"><span class="pagepre"></span></a></li>';

      //定义开始位置begin和结束位置end
      var begin;
      var end;
      if (rel.data.totalPage<5){
        //总页码不够5页
        begin = 1;
        end = rel.data.totalPage;
      }else {
        //总页码超过5页
        begin = rel.data.pageNum - 1;
        end = rel.data.pageNum + 3;
        if (begin<=1){
          begin = 1;
          end = begin + 4;
        }
        if (end >= rel.data.totalPage){
          end = rel.data.totalPage;
          begin = end - 4;
        }
      }

      for (let i = begin; i <= end; i++) {
        let li = '';
        if (i===rel.data.pageNum){
          li='<li class="paginItem current"><a href="javascript:;" onclick="queryPurchase('+currentPage+')">'+currentPage+'</a></li>';
        }else {
          li='<li class="paginItem"><a href="javascript:;" onclick="queryPurchase('+i+')">'+i+'</a></li>';
        }
        strbu += li;
      }

      let nextNum;
      if (currentPage>=rel.data.totalPage){
        nextNum = rel.data.totalPage;
      }else {
        nextNum = currentPage+1;
      }
      strbu += '        <li class="paginItem"><a href="javascript:;" onclick="queryPurchase('+nextNum+')"><span class="pagenxt"></span></a></li>';
      $("#pageList").html(strbu);
    }
  })
}
</script>

</head>

<body>
<div class="place"> <span>位置：</span>
  <ul class="placeul">
    <li>采购管理</li>
    <li>采购单管理</li>
    <li>基本内容</li>
  </ul>
</div>
<div class="rightinfo">
  <form action="" method="post" id="purchaseForm">
    <ul class="tools">
      <li> 采购单编号:
        <input type="text" id="purchase_ID"/>
      </li>
      <li> 采购时间:
        <input type="date" id="preTime"/>-<input type="date" id="afterTime"/>
      </li>
      <li> 金额:
        <input type="text" class="stinput" id="preMoney"/>-<input type="text" class="stinput" id="afterMoney"/>
      </li>
      <li> 审核状态：
        <select id="check_Status">
          <option>请选择</option>
          <option value="1">未审核</option>
          <option value="2">审核中</option>
          <option value="3">审核通过</option>
          <option value="4">审核未通过</option>
        </select>
      </li>
      <li class="subBut" onclick="search()"><img src="../../../static/images/t06.png" />查询</li>
      <li class="subBut" onclick="window.location.href='purchase/purchaseAdd'"><img src="../../../static/images/t01.png" />添加</li>
    </ul>
    <table class="tablelist">
      <thead>
        <tr>
          <th>序号</th>
          <th>采购单编号</th>
          <th>采购时间</th>
          <th>金额</th>
          <th>操作人</th>
          <th>审核状态</th>
          <th>审核人</th>
          <th>审核时间</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody id="purchaseBody">
        <tr>
          <td>1</td>
          <td>CG201701270001</td>
          <td>2017-01-25 15:05:05</td>
          <td>￥9,876,582</td>
          <td>关羽</td>
          <td>未审核</td>
          <td></td>
          <td></td>
          <td>
            <a href="purchaseUpdate.html" class="tablelink">修改</a>
            <a href="javascript:void(0);" class="tablelink" onclick="deltipOpen()">删除</a>
            <a href="javascript:void(0);" class="tablelink" onclick="examinetipOpen()">提交审核</a>
            </td>
        </tr>
        <tr>
          <td>2</td>
          <td>CG201701270002</td>
          <td>2017-01-25 15:05:05</td>
          <td>￥9,876,582</td>
          <td>关羽</td>
          <td>审核中</td>
          <td></td>
          <td></td>
          <td>
          	<a href="purchaseView.html" class="tablelink">查看详情</a>
          </td>
        </tr>
        <tr>
          <td>3</td>
          <td>CG201701270003</td>
          <td>2017-01-25 15:05:05</td>
          <td>￥9,876,582</td>
          <td>关羽</td>
          <td>审核通过</td>
          <td>曹操</td>
          <td>2017-01-30 12:05:05</td>
          <td>
          	<a href="purchaseView.html" class="tablelink">查看详情</a>
            <a href="../../storage/stock/stockView.html" class="tablelink">入库详情</a>
          </td>
        </tr>
        <tr>
          <td>4</td>
          <td>CG201701270004</td>
          <td>2017-01-25 15:05:05</td>
          <td>￥9,876,582</td>
          <td>关羽</td>
          <td>审核不通过</td>
          <td>曹操</td>
          <td>2017-01-30 12:05:05</td>
          <td>
            <a href="purchaseUpdate.html" class="tablelink">修改</a>
            <a href="javascript:void(0);" class="tablelink" onclick="examinetipOpen()">提交审核</a>
          </td>
        </tr>
      </tbody>
    </table>
    <div class="pagin" id="pageList">
      <div class="message">共<i class="blue">1256</i>条记录，当前显示第&nbsp;<i class="blue">2&nbsp;</i>页</div>
      <ul class="paginList">
        <li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>
        <li class="paginItem"><a href="javascript:;">1</a></li>
        <li class="paginItem current"><a href="javascript:;">2</a></li>
        <li class="paginItem"><a href="javascript:;">3</a></li>
        <li class="paginItem"><a href="javascript:;">4</a></li>
        <li class="paginItem"><a href="javascript:;">5</a></li>
        <li class="paginItem more"><a href="javascript:;">...</a></li>
        <li class="paginItem"><a href="javascript:;">10</a></li>
        <li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>
      </ul>
    </div>
  </form>
  <!-- 删除提示框 -->
  <div id="deltip" class="tip">
    <div class="tiptop"><span>提示信息</span><a onclick="deltipClose()"></a></div>
    <div class="tipinfo"> <span><img src="../../../static/images/ticon.png" /></span>
      <div class="tipright">
        <p>是否确认删除此条信息？</p>
        <cite>如果是请点击确定按钮 ，否则请点取消。</cite> </div>
    </div>
    <div class="tipbtn" id="pdl_Dtip">
      <input name="" type="button"  class="sure" value="确定" onclick="deltipClose()" />
      &nbsp;
      <input name="" type="button"  class="cancel" value="取消" onclick="deltipClose()" />
    </div>
  </div>
  
  <!-- 审批提示框 -->
  <div id="examinetip" class="tip">
    <div class="tiptop">
    	<span>提示信息</span><a onclick="examinetipclose()"></a>
    </div>
    <div class="tipinfo1">
        部门：
            <select class="deptId" onchange="deptChangeP()">
            	<option value="">请选择</option>
<!--                <option>销售部</option>-->
<!--                <option>采购部</option>-->
<!--                <option>财务部</option>-->
            </select>
			<p/>
            职位：
            <select class="jobId" onchange="jobChangeP()">
            	<option value="">请选择</option>
<!--                <option>部门经理</option>-->
<!--                <option>部门副经理</option>-->
<!--                <option>部门主管</option>-->
            </select>
   			<p/>
            审核人：
            <select class="userId">
            	<option value="">请选择</option>
<!--                <option>刘备</option>-->
<!--                <option>曹操</option>-->
            </select>
			<p/>
    </div>
    <div class="tipbtn" id="pdl_Utip">
      <input name="" type="button"  class="sure" value="确定" onclick="examinetipclose()" />
      &nbsp;
      <input name="" type="button"  class="cancel" value="取消" onclick="examinetipclose()" />
    </div>
  </div>
</div>
<script type="text/javascript">
	$('.tablelist tbody tr:odd').addClass('odd');
</script>
</body>
</html>
